import "./style.css"
import {
    STActor,
    AdvanceSTActor,
    BdfLoader,
    Application,
    BdfActor,
    VTActorRenderType,
    calculateNormalFromFourPoints,
    worldToDisplay,
    IMouseEvent,
    StandardType,
    LinesActor,
    LoaderCachePlugin,
    AxisActor,
    VTLinesActor,
    GyMesh3dLoader,
    VTActor,
    GyMesh3DActor,
    NormalActor,
    LabelActor,
    vtkReader,
} from "../lib"
import { Vector3 } from "stl-typescript"
let dom = document.getElementById("app") || document.body
import test3dMeshData from "./gymesh3d_test.json"

const app = new Application(dom, {})
setTimeout(async () => {
    // app.tryFaceToCell(new Vector3(0, 1, 0))
    const m = 1020

    function testB() {
        app.api.system.clear()
    }

    // * 测试选择器
    async function testSelector() {
        const loader = await new BdfLoader("")
            .use(new LoaderCachePlugin())
            .load("http://localhost:3000/temp/out2.json")
        const ac = new BdfActor(loader.polyData)
        ac.setRenderType(VTActorRenderType.SURFACE_WITH_EDGE)
        // ac.setCellColor(1, [255, 0, 0, 255])
        app.registerMouseRightListener((event: IMouseEvent) => {
            if (event.shiftKey) return
            const r = app.selector.pickCell(event.position.x, event.position.y)
            ac.setCellColor(r, [0, 255, 0, 255])
            console.log(loader.getCellInfoByIndex(r))
            app.renderAtOnce()
        })
        app.selector.selectBox.onchange = (_1, _2, _3, _4, altKey) => {
            app.selector.preComputeDisplayPos(loader)
            app.selector.pickCellsInRectangleBox(!altKey).then((r: any) => {
                const z = r.map((i, index) => {
                    ac.setCellColor(i.cellIndex, [0, 255, 0, 255])
                })
                ac.setCellsColor(z, [0, 255, 0, 255])
                app.renderAtOnce()
            })
        }
        app.add(ac)
    }

    async function testD() {
        app.api.scene.create("ReportScene")
        const loader = await new BdfLoader().load(
            "http://localhost:3000/temp/out2.json"
        )
        const ac = new STActor(
            {
                type: StandardType.VEC1,
                scalarBarTitle: "测试数据",
                name: "测试",
                eid: [2133338, 2133339, 2133823],
                value: [1, 2, 3],
            },
            loader
        ) as AdvanceSTActor
        ac.createLabels([
            {
                key: "max",
                value: "2333",
                position: "left-top",
                style: {
                    color: "red",
                },
            },
            {
                key: "min",
                value: "-233",
                position: "left-top",
            },
        ])
        app.reportScene?.add(ac)
        app.reportScene?.showOnly(0)
    }

    // 测试纯线条
    function testLines() {
        const ac = new LinesActor([
            0, 0, 0, 100, 100, 100, 100, 100, 100, 0, 100, 0,
        ])
        app.add(ac)
    }

    function testAxisActor() {
        const ac = new AxisActor()
        app.add(ac)
        app.updateCamera()
        app.renderAtOnce()
    }

    async function testSTDiff() {
        app.api.scene.create("ReportScene")
        const loader = await new BdfLoader().load(
            "http://localhost:3000/temp/out2.json"
        )
        const ac = new STActor(
            {
                type: StandardType.DIFF,
                name: "测试",
                value: {
                    rough: [2133338],
                    smooth: [2133339, 2133823],
                },
            },
            loader
        ) as AdvanceSTActor
        app.reportScene?.add(ac)
        app.reportScene?.showOnly(0)
    }

    async function testBDFLoader() {
        const loader = await new BdfLoader()
            .use(new LoaderCachePlugin())
            .load(
                "http://localhost:9000/d3-engine/bdf/02ede948-351c-4a2d-b618-3fce0e07bfa6tmpbdf.json"
            )
        const ac = new BdfActor(loader.polyData)
        ac.setRenderType(VTActorRenderType.SURFACE_WITH_EDGE)
        const linesAc = new LinesActor(loader.lineTypeData)
        app.add(linesAc)
        app.add(ac)
    }

    async function testBarSelector() {
        const loader: BdfLoader = await new BdfLoader()
            .use(new LoaderCachePlugin())
            .load(
                "http://localhost:9000/d3-engine/bdf/02ede948-351c-4a2d-b618-3fce0e07bfa6tmpbdf.json"
            )
        const ac = new BdfActor(loader.polyData)
        ac.setRenderType(VTActorRenderType.SURFACE_WITH_EDGE)
        // const linesAc = new VTLinesActor(loader.linePolyData)
        app.registerMouseRightListener((event: IMouseEvent) => {
            if (event.shiftKey) return
            // const s = app.selector.pickEdge(event.position.x, event.position.y, 0.05)
            // s && s.forEach((i) => {
            //   console.log(i)
            //   linesAc.setCellColor(i, [0, 255, 0, 255])
            //   app.renderAtOnce()
            // })
            app.selector.pickCell(
                event.position.x,
                event.position.y,
                (idx, acIndex) => {
                    console.log("选择到的单元", idx, "acIndex", acIndex)
                    const res = app.selector.pickCellByFaceForBdf(
                        ac,
                        loader,
                        idx,
                        5
                    )
                    ac.setCellColor(idx, [0, 255, 0, 255])
                    res.forEach((idx2) => {
                        ac.setCellColor(idx2, [0, 255, 0, 255])
                    })
                    app.renderAtOnce()
                }
            )
        })
        // app.add(linesAc)
        app.add(ac)
        // app.selector.removeFromTargetList(linesAc)
        // app.selector.removePointTargetList(linesAc)
    }

    async function testMesh3dLoader() {
        const loader = new GyMesh3dLoader()
        await loader.load(
            "http://127.0.0.1:9000/d3-engine/temp/3dmesh_test.json"
        )
        const ac = new GyMesh3DActor(loader)
        ac.setRenderType(VTActorRenderType.SURFACE_WITH_EDGE)
        app.add(ac)
    }

    function testNormalSActor() {
        const ac = new NormalActor([0, 0, 0, 10, 0, 0], [0, 1, 0, 0, 1, 0])
        app.add(ac)
        const label  = new LabelActor([0,0,0],{
            text: "test",
            scale: true
        })
        app.add(label)
        setTimeout(()=>{
          ac.setScaleFactor(1)
          app.render()
        },500)
    }

    async function testVtkActor() {
        const reader = new vtkReader()
        await reader.read("http://localhost:9000/test/model2.vtk")
        console.log(reader.attributes)
        const loader = new BdfLoader()
        loader.parse(reader.getStandardJsonObject())
        const ac = new BdfActor(loader.polyData)
        ac.setRenderType(VTActorRenderType.SURFACE_WITH_EDGE)
        ac.setScalars(reader.attributes["displacement"].Magnitude)
        const sc =ac.scalarBar0n()
        app.renderer.addActor(sc) 
        app.add(ac)
    }
    // testC();
    // testD();
    // testLines()
    // testAxisActor()
    // testBDFLoader()
    // testMesh3dLoader()
    // testNormalSActor()
    testVtkActor()
}, 2000)
